package controller;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.FirendService;
import utils.Page;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//好友列表
@Controller
@RequestMapping("/friend")
public class FirendController {

    @Autowired
    private FirendService service;

    //好友列表查询
    @RequestMapping("/friends")
    public String friends(@RequestParam(value = "uid",defaultValue = "1")int uid,
                          @RequestParam(value = "uname",defaultValue = "1")String uname,
                          @RequestParam(value = "curPage",defaultValue = "0")Integer curPage,
                          @RequestParam(value = "pageSize",defaultValue = "4") Integer pageSize,
                          Model model, HttpSession session){

        uid=((User)session.getAttribute("sos")).getUid();
        int count=service.getCounta(uid);
        //分类工具对象
        Page<User> page=new Page<User>(curPage,pageSize,count);
        //获得集合
        List<User> list=service.getUserList(uid,uname,page.getBeginPos(),pageSize);
        page.setRows(list);
        model.addAttribute("page",page);
        model.addAttribute("count",count);

        return "user-friends";
    }

    //用户列表查询
    @RequestMapping("/groups")
    public String groups(@RequestParam(value = "chaname",defaultValue = "")String chaname,
                         @RequestParam(value = "curPage",defaultValue = "0")Integer curPage,
                         @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize,
                          Model model, HttpSession session){
        int id=((User)session.getAttribute("sos")).getUid();

        List<Integer> l=service.z_selectfriend(id);
        l.add(id);
        int count=service.getCount(chaname);
        count=count-l.size();
        //分类工具对象
        Page<User> page=new Page<User>(curPage,pageSize,count);
        //获得集合
        List<User> list=service.getUserLists(chaname,page.getBeginPos(),pageSize,l);


        /*for (int i=0;i<list.size();i++){
            for (int j=0;j<l.size();j++){
                if(list.get(i).getUid()==l.get(j)){
                    list.remove(i);
                    break;
                }
            }
        }*/



        page.setRows(list);

        model.addAttribute("page",page);
        model.addAttribute("count",count);
        model.addAttribute("chaname",chaname);
        return "user-groups";
    }

    //查id
    @RequestMapping("/update")
    public String update(int uid,Model model){
        User user = service.getUserId(uid);
        model.addAttribute("user",user);
        return "pass";
    }

    //修改密码
    @RequestMapping("/pass")
    public String pass(String newpass,Model model,HttpSession session){
       int uid=((User)session.getAttribute("sos")).getUid();

       User u=new User();
       u.setUpwd(newpass);
       u.setUid(uid);

        int users = service.getUpdate(u);


        if (users>0){
            return "redirect:/Directory/hou";
        }
        return "redirect:/Directory/pass";
    }

    //用户列表查询2
    @RequestMapping("/book")
    public String book(@RequestParam(value = "chaname",defaultValue = "")String chaname,
                         @RequestParam(value = "curPage",defaultValue = "1")Integer curPage,
                         @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize,
                         Model model,HttpSession session){
        int count=service.getCount(chaname);
        //分类工具对象
        Page<User> page=new Page<User>(curPage,pageSize,count);
        //获得集合
        List<Integer> li=new ArrayList<>();
        int id=((User)session.getAttribute("sos")).getUid();

        li.add(id);
        List<User> list=service.getUserLists(chaname,page.getBeginPos(),pageSize, li);
        page.setRows(list);
        model.addAttribute("page",page);
        model.addAttribute("count",count);
        model.addAttribute("chaname",chaname);
        return "HT/book";
    }

    //删除用户信息
    @RequestMapping("/del")
    public String del(int uid,Model model){
      int n=service.delUser(uid);

        if(n>0){
            //删除成功

        }
        else{

            //删除失败
        }
        return "redirect:/friend/book";
    }

    //添加好友
    @RequestMapping("tianyou")
    public  String tianyou(int id,HttpSession session){
       User u= (User)session.getAttribute("sos");
       int i= u.getUid();

       int num=service.tianyou(i,id);

        if(num==1){
            //成功
        }
        else{
            //失败
        }
        return "redirect:/friend/friends";
    }

    @RequestMapping("shanyou")
    public  String shanyou(int id,HttpSession session){


        int num=service.shanyou(id);

        if(num==1){
            //成功
        }
        else{
            //失败
        }

        return "redirect:/friend/groups";
    }

}
